package com.cloud.bssp.netty.promise;

import io.netty.channel.DefaultEventLoop;
import io.netty.util.concurrent.DefaultPromise;

import java.util.concurrent.ExecutionException;

/**
 * @description： Promise异步处理任务成功
 * @author：weirx
 * @date：2021/11/10 14:18
 * @version：3.0
 */
public class Test2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        DefaultEventLoop eventExecutors = new DefaultEventLoop();
        DefaultPromise<Integer> promise = new DefaultPromise<>(eventExecutors);

        eventExecutors.execute(()->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() +", set success: " + 10);
            promise.setSuccess(10);
        });

        System.out.println(Thread.currentThread().getName() +", start...");

        //添加异步监听，当有结果时调用getNow获取结果
        promise.addListener(future->{
            System.out.println(Thread.currentThread().getName() +", " + future.getNow());
        });
    }
}
